package com.huazhi.wzsnw.util.tcp;

import com.huazhi.wzsnw.dtu.DtuServer;
import com.huazhi.wzsnw.mapper.AlertMapper;
import com.huazhi.wzsnw.mapper.AlertValueMapper;
import com.huazhi.wzsnw.mapper.LinedMapper;
import com.huazhi.wzsnw.mapper.LinedStateMapper;
import com.huazhi.wzsnw.model.Alert;
import com.huazhi.wzsnw.model.AlertValue;
import com.huazhi.wzsnw.model.Lined;
import com.huazhi.wzsnw.service.AlertValueService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class StartListen {

    @Autowired
    LinedMapper linedMapper;
    @Autowired
    LinedStateMapper linedStateMapper;
    @Autowired
    AlertValueService alertValueService;
    @Autowired
    AlertMapper alertMapper;
    @Autowired
    AlertValueMapper alertValueMapper;

    public void initialize() {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);
//        initAlertValue();

        fixedThreadPool.execute(new Runnable() {
            public void run() {
                DtuServer dtuServer = new DtuServer();
                dtuServer.bind();
//                try {
//                    //创建一个Server类
//                    Server server=new Server();
//                    try {
//                        //启动服务器
//                        //server.start(linedMapper);
//                        server.start(linedMapper,linedStateMapper,alertMapper,alertValueMapper);
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }
        });
        //serverSocket.close();一直循环监听，不用关闭连接
    }

    public void initAlertValue(){
        List<String> list = alertValueService.getSearchAlertValueList(null);
        if(!list.isEmpty()){
            List<AlertValue> alertValueList = alertValueService.findPagingByListTid(list,null,null);
            for (AlertValue alertValue:alertValueList){
                alertValueService.updateAlertValueState(alertValue);
            }
        }

    }
}
